<template>
  <h4>x的值:{{ sum }}</h4>
  <button type="" @click="sum++">点击x值+1</button>
  <hr />
  <h2>{{ `姓名：${name}` }}</h2>
  <h2>年龄： {{ age }}</h2>
  <h2>薪水：{{ job.j1.salary }}K</h2>
  <button type="" @click="name += '~'">修改姓名</button>
  <button type="" @click="age++">增长年龄</button>
  <button type="" @click="job.j1.salary++">涨薪</button>
  <hr />
  <h4>座驾信息:{{ car }}</h4>
  <button @click="showRawPerson">原始的person</button>
  <button @click="addCar">增加一台车</button>
  <button @click="car.name +='!'" v-show="car.name">修改车的名称</button>
  <button @click="carPriceIncrease" v-show="car.price">修改车的价格</button>
</template>

<script>
// ref, reactive,
import { ref, reactive, toRefs, toRaw ,markRaw} from "vue";
export default {
  name: "App",
  setup() {
    let sum = ref(0);

    let person = reactive({
      name: "张三",
      age: 18,
      job: {
        j1: {
          salary: 15,
        },
      },
      car:{}
    });

    let s;
    function showRawPerson() {
      // toRaw 将一个reactive数据，变成普通的数据(非响应式)
      const p = toRaw(person)
      p.age++
      console.log('p :>> ', p);
      // s = toRaw(sum);
      // s.value++;
      // console.log("s :>> ", s);
    }

    function addCar(){
      const car = {name:'奔驰',price:50}
      // person.car = car
      //作用：标记一个对象，使其永远不会再成为响应式对象。
      person.car = markRaw(car)
      
      console.log('person :>> ', person);
    }

    function carPriceIncrease(){
        person.car.price ++
        console.log('person.car.price :>> ', person.car.price);
    }

    return {
      sum,
      person,
      s,
      ...toRefs(person),
      showRawPerson,
      addCar,
      carPriceIncrease
    };
  },
};
</script>
